//
// Created by jetkey on 18-8-13.
//

#include <jni.h>

#define __android

#include "testsuit_framework.h"

#include "log_helper.hpp"

using Printer = std::function<void (const string &)>;

typedef void(*TestFun)(Printer p);

struct TestCase{
    const char * name;
    TestFun run;
};

extern TestCase testList[];

void myprint(JNIEnv *env,jobject me, const string &text){
    jclass clazz = env->GetObjectClass(me);  // env->FindClass("com/imi/sdk/jnitest/MainActivity");
    jmethodID meth = env->GetMethodID(clazz,"print","(Ljava/lang/String;)I");
    //
    jstring  str = env->NewStringUTF(text.c_str());
    env->CallIntMethod(me,meth,str);
    //
    env->DeleteLocalRef(str);
    env->DeleteLocalRef(clazz);
}


JNIEXPORT jint JNICALL getTestCount(JNIEnv *env,jobject me){
    TestCase *p = testList;
    int i=0;
    while(p->run!=NULL){
        i++;
        p++;
        LOGDS("i=",i)
    }
    return i;
}

JNIEXPORT jstring JNICALL getTestName(JNIEnv *env,jobject me,jint i){
    TestCase &testCase = testList[i];
    jstring res = env->NewStringUTF(testCase.name);
    return res;
}

JNIEXPORT jint JNICALL runTest(JNIEnv *env,jobject me,jint i){
    TestCase &testCase = testList[i];

    function<void (const string &)> f = std::bind(myprint,env,me,std::placeholders::_1); //(env,me);
    //
    testCase.run(f);
    //
    return 0;
}



#include "testsuit_framework.h"
